import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:convex_bottom_bar/convex_bottom_bar.dart';
import 'package:sites_hub/pages/page_manager.dart';

class HorizonNavigation extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HorizonNavigationState();
  }
}

class _HorizonNavigationState extends State<HorizonNavigation> {
  var _currentHeight = _initHeight;
  static const double _initHeight = 20.0;
  static const double _hoverHeight = 60.0;
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      onEnter: (e) {
        setState(() {
          _currentHeight = _hoverHeight;
        });
      },
      onExit: (e) {
        setState(() {
          _currentHeight = _initHeight;
        });
      },
      child: AnimatedContainer(
        height: _currentHeight,
        // color: Colors.white.withOpacity(0.3),
        duration: const Duration(milliseconds: 0),
        child: _currentHeight == _initHeight
            ? Container(
                color: Colors.blue,
                width: MediaQuery.of(context).size.width,
                child: Center(child: Text('hover it')),
              )
            : Transform.scale(
                scaleY: _currentHeight == _initHeight ? 0.5 : 1,
                child: ConvexAppBar(
                  initialActiveIndex: _currentIndex,
                  items: PageManager.items(),
                  onTap: (int i) {
                    var name = PageManager.list[i].pageName;
                    context.read<IndexedIndicator>().go(name);
                    setState(() {
                      _currentIndex = i;
                    });
                  },
                ),
              ),
      ),
    );
  }
}

class IndexedIndicator with ChangeNotifier {
  String _path = 'site_page';
  int _index = 0;

  int get index => _index;

  String get page => _path;

  void go(String path) {
    _path = path;
    notifyListeners();
  }

  void next() {
    if (index != 0) {
      return;
    }
    _index++;
    notifyListeners();
  }

  void prev() {
    if (index != 1) {
      return;
    }
    _index--;
    notifyListeners();
  }
}
